package controller;

import dao.UserDaoImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import textMode.PhoneCode;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 简介说明: 发送验证验证码
 *
 * @author: Half_Q_0115
 * @date: 2024/09/18 15:44:21
 * @version: 1.0
 */
@WebServlet(urlPatterns = "/phoneServlet")
public class PhoneServlet extends HttpServlet {
    private PhoneCode phoneCode = new PhoneCode();
    // 用于存储手机号与验证码的映射
    private Map<String, String> phoneCodeMap = new HashMap<>();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String method = req.getParameter("method");
        if ("sendMessage".equals(method)) {
            this.sendMessage(req, resp);
        } else if ("verify".equals(method)) {
            this.verify(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    private void sendMessage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String phoneNumber = req.getParameter("phone_number");
        if (phoneCode.isPhoneNumber(phoneNumber)) {
            String generatedCode = phoneCode.getPhonemsg(phoneNumber);
            if ("true".equals(generatedCode)) {
                phoneCodeMap.put(phoneNumber, phoneCode.getCode());
                resp.getWriter().write("true");
            } else {
                resp.getWriter().write("发送验证码失败");
            }
        } else {
            resp.getWriter().write("手机号格式不正确");
        }
    }

    private void verify(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String phoneNumber = req.getParameter("phone_number");
        String inputCode = req.getParameter("captcha");
        UserDaoImpl userDao = new UserDaoImpl();
        System.out.println(inputCode);
        if (!userDao.checkPhone(phoneNumber)) {
            resp.getWriter().write("手机号不存在");
        } else {
            // 获取存储的验证码
            String storedCode = phoneCodeMap.get(phoneNumber);
            if (storedCode == null || !storedCode.equals(inputCode)) {
                resp.getWriter().write("验证码错误");
            } else {
                // 验证通过，设置用户信息到 session
                HttpSession session = req.getSession();
                session.setAttribute("id", userDao.getIdByPhone(phoneNumber));
                session.setAttribute("identity", userDao.getIdentityByPhone(phoneNumber));
                session.setAttribute("name", userDao.getNameByPhone(phoneNumber));
                session.setAttribute("college_id", userDao.getCollegeIdByPhone(phoneNumber));
                resp.getWriter().write("true");
            }
        }
    }
}
